﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using NavigationShowcase.Data;
using WinRTFramework.Common;
using WinRTFramework.Interfaces.AppStates;
using WinRTFramework.Interfaces.Navigation;
using WinRTFramework.Interfaces.Navigation.ViewModel;
using Windows.UI.Xaml.Data;

namespace NavigationShowcase.ViewModels
{
    public class ItemDetailPageViewModel_Showcase : Bindable, ICareAboutNavigationFlow, IRequireNavigationService, ISupportTransientState, ICareAboutSuspendResume
    {
        private SampleDataItem _selectedItem;
        private SampleDataGroup _group;
        private IEnumerable<SampleDataItem> _items;
        private string _transientText;

        public ItemDetailPageViewModel_Showcase()
        {
            TransientText = "Initial Transient Text";
            InfoText = "Initial Info Textus";
        }

        public SampleDataGroup Group
        {
            get { return _group; }
            set { _group = value; NotifyPropertyChanged(() => Group); }
        }

        public string TransientText
        {
            get { return _transientText; }
            set { _transientText = value; NotifyPropertyChanged(() => TransientText); }
        }

        private string _infoText;
        public string InfoText
        {
            get { return _infoText; }
            set { _infoText = value; NotifyPropertyChanged(() => InfoText); }
        }

        private ICollectionView _itemsView;
        public ICollectionView ItemsView
        {
            get { return _itemsView; }
            set { _itemsView = value; NotifyPropertyChanged(() => ItemsView); }
        }

        void LoadFromNavigationParameter(object navigationParameter)
        {
            LoadItem((string)navigationParameter);
        }

        void LoadItem(string itemId)
        {
            var item = SampleDataSource.GetItem(itemId);
            Group = item.Group;

            ItemsView = new CollectionViewSource() { Source = item.Group.Items }.View;
            ItemsView.MoveCurrentTo(item);
        }

        #region ICareAboutSuspendResume
        public void Suspending()
        {
            
        }

        public void Resumed(object navigationParameter, ResumePreviousAppState previousAppState)
        {
            InfoText = "Resumed! " + previousAppState;

            if(previousAppState == ResumePreviousAppState.Terminated)
                LoadFromNavigationParameter(navigationParameter);
        }
        #endregion

        #region ISupportTransientState
        public IEnumerable<Type> GetTransientStateTypes()
        {
            yield return typeof(TransientState);
        }

        public void UpdateTransientState(object untypedState)
        {
            TransientState state = (TransientState)untypedState;
            TransientText = state.TransientText;
            ItemsView.MoveCurrentTo(Group.Items.First(k => k.UniqueId == state.SelectedItemId));
        }

        public object GetTransientState()
        {
            return new TransientState() { TransientText = TransientText, SelectedItemId = ((SampleDataItem)ItemsView.CurrentItem).UniqueId };
        }

        [DataContract]
        public class TransientState
        {
            [DataMember]
            public string TransientText { get; set; }

            [DataMember]
            public string SelectedItemId { get; set; }
        }
        #endregion

        #region ICareAboutNavigationFlow
        public void OnNavigatedTo(object navigationParameter)
        {
            LoadFromNavigationParameter(navigationParameter);
        }

        public void OnNavigatingFrom()
        { }

        public void OnNavigatedFrom()
        { }
        #endregion

        #region IRequireNavigationService
        INavigationService _navigationService;
        public INavigationService NavigationService
        {
            get { return _navigationService; }
            set { _navigationService = value; }
        }
        #endregion

    }
}
